﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMC;
using PMC.Urls;

namespace PMCgui
{
    class WebbrowserMenustrip : StandardMenuStrip
    {
        protected ToolStripMenuItem OpenSession = new ToolStripMenuItem(Language.Instance.OpenSession);
        protected ToolStripMenuItem SaveSession = new ToolStripMenuItem(Language.Instance.SaveSession);
        protected ToolStripMenuItem OpenOld = new ToolStripMenuItem(Language.Instance.OpenFile);

        public WebbrowserMenustrip()
        {
            // File //
            {
                File.DropDownItems.Add("-");
                File.DropDownItems.Add(OpenSession);
                File.DropDownItems.Add(SaveSession);
                File.DropDownItems.Add("-");
                File.DropDownItems.Add(OpenOld);
            }
            // Settings
            {
                Settings.Available = false;
            }
            //Handlers
            OpenSession.Click += OpenSession_Click;
            SaveSession.Click += SaveSession_Click;
            OpenOld.Click += OpenOld_Click;
        }

        void OpenOld_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                Filter = "All files (*.*) | *.*",
                RestoreDirectory = true,
            };

            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            TextReader text = null;
            try
            {
                //Open file
                text = new StreamReader(dialog.FileName);
                var line = text.ReadLine();

                List<Uri> uris = new List<Uri>(); //List to store uris in

                while (line != null) //Loop through file
                {
                    if (Uri.IsWellFormedUriString(line, UriKind.Absolute)) //Check if line isWellFormed-uri
                    {
                        Uri newurl;
                        if (Uri.TryCreate(line, UriKind.Absolute, out newurl)) //Tries to create an uri
                            uris.Add(newurl); //Adds it to the list
                    }
                    line = text.ReadLine(); //Reads next line               
                }
                UriCollector.ActiveCollector.LoadUri(uris); //Loads them
            }
            catch (Exception ex)
            {
                Log.Instance.AddException(ex);
            }
            finally
            {
                if (text != null) text.Close();                
            }
        }

        void SaveSession_Click(object sender, EventArgs e)
        {
            var dialog = new SaveFileDialog
            {
                Filter = Language.Instance.PMCSessionFiles + " | " + PMC.Properties.Settings.Default.SessionFileExtension,
                RestoreDirectory = true,
            };

            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            var uris = UriCollector.ActiveCollector.ToList();
            Global.XMLSerialize(dialog.FileName, uris);
        }

        void OpenSession_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog
                             {
                                 Multiselect = false,
                                 Filter = Language.Instance.PMCSessionFiles + " | " + PMC.Properties.Settings.Default.SessionFileExtension,
                                 RestoreDirectory =  true,
                             };

            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            var list = Global.XMLDeserialize<IList<Uri>>(dialog.FileName);
            UriCollector.ActiveCollector.LoadUri(list);
        }
    }
}
